package com.landinfo.service;


import org.apache.commons.codec.binary.Base64;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;

/**
 * Created by ziqilv on 2017/6/19.
 */
public class ImageHelper {
    //static BASE64Encoder encoder = new BASE64Encoder();
    //static BASE64Decoder decoder = new BASE64Decoder();

    static Base64 base64= new Base64();

    // 将 file 转化为 Base64
    public static String fileToBase64(String path) {
        File file = new File(path);
        FileInputStream inputFile;
        try {
            inputFile = new FileInputStream(file);
            byte[] buffer = new byte[(int) file.length()];
            inputFile.read(buffer);
            inputFile.close();

            return new String(base64.encodeBase64(buffer)) ;
            //return new BASE64Encoder().encode(buffer);
        } catch (Exception e) {
            throw new RuntimeException("文件路径无效\n" + e.getMessage());
        }
    }

    public static boolean base64StringToImage(String base64String, String filePath) {
        try {
//            byte[] bytesContent = decoder.decodeBuffer(base64String);
//            ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytesContent);
//            BufferedImage bufferedImage = ImageIO.read(byteArrayInputStream);
//            File newFile = new File(filePath);
//            File directory = new File(newFile.getParent());
//            if (!directory.exists()) {
//                directory.mkdirs();
//            }
//            BufferedImage newBufferedImage = new BufferedImage(bufferedImage.getWidth(), bufferedImage.getHeight(), BufferedImage.TYPE_3BYTE_BGR);
//            newBufferedImage.getGraphics().drawImage(bufferedImage, 0, 0, null);
//            ImageIO.write(bufferedImage, "jpg", newFile);


//            byte[] bytesContent = decoder.decodeBuffer(base64String);
            byte[] bytesContent =base64.decode(base64String);   // decoder.decodeBuffer(base64String);
            ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytesContent);
            BufferedImage bufferedImage = ImageIO.read(byteArrayInputStream);
            File newFile = new File(filePath);
            File directory = new File(newFile.getParent());
            if (!directory.exists()) {
                directory.mkdirs();
            }
            int width=bufferedImage.getWidth();
            int height=bufferedImage.getHeight();

            BufferedImage to = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
            Graphics2D g2d = to.createGraphics();
            to = g2d.getDeviceConfiguration().createCompatibleImage(width,height, Transparency.TRANSLUCENT);
            g2d.dispose();

            g2d = to.createGraphics();
            Image from = bufferedImage.getScaledInstance(width, height, Image.SCALE_AREA_AVERAGING);
            g2d.drawImage(from, 0, 0, null);
            g2d.dispose();
            ImageIO.write(to, "png", newFile);
            return true;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }


    public static void saveJpgImage(BufferedImage image, String imgType, File filePath) {
        BufferedImage newBufferedImage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_3BYTE_BGR);
        newBufferedImage.getGraphics().drawImage(image, 0, 0, null);
        try {
            ImageIO.write(image, imgType, filePath);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }


    public static String getImageBinary(String file) {
        File f = new File(file);
        try {
            BufferedImage bi = ImageIO.read(f);
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            ImageIO.write(bi, "jpg", baos);
            byte[] bytes = baos.toByteArray();

            //return encoder.encodeBuffer(bytes).trim();
            return base64.encodeToString(bytes);    // encoder.encodeBuffer(bytes).trim();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
